package nl.hva.three2learn.authentication;

import java.util.Collection;
import java.util.List;

import nl.hva.three2learn.model.User;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

public class UserAuthentication implements Authentication {

	private static final long serialVersionUID = 1L;
	
	private final UserAuthenticationDetails principal;
	
	public UserAuthentication(UserAuthenticationDetails principal) {
		this.principal = principal;
	}
	
	@Override
	public String getName() {
		return principal.getUsername();
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return principal.getAuthorities();
	}
	
	public boolean isInRole(Role role) {
		for (GrantedAuthority auth : principal.getAuthorities()) {
			if (auth.getAuthority().equals(role.toString()))
				return true;
		}
		
		return false;
	}
	
	@Override
	public Object getCredentials() {
		return principal.getPassword();
	}

	@Override
	public Object getDetails() {
		return principal.getUser();
	}

	@Override
	public Object getPrincipal() {
		return principal;
	}

	public User getUser() {
		return principal.getUser();
	}
	
	@Override
	public boolean isAuthenticated() {
		return true;
	}

	@Override
	public void setAuthenticated(boolean flag) throws IllegalArgumentException {
		
	}
}
